package com.attms.service.impl;

import com.attms.controller.utils.R;
import com.attms.dao.AbsentDao;
import com.attms.dao.EmployeeDao;
import com.attms.domain.Absent;
import com.attms.domain.Employee;
import com.attms.domain.AbsentStrategy.AbsentStrategySet;
import com.attms.domain.EmployeeFactory.EmployeeFactory;
import com.attms.service.IEmployeeService;
import com.attms.service.IPersonnelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class PersonnelServiceImpl extends ServiceImpl<EmployeeDao, Employee> implements IPersonnelService {
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private AbsentDao absentDao;

    public List<Employee> getAllInfo() {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.select("E_ID", "E_NAME", "E_GENDER", "E_BIRTH", "E_START", "E_JOB", "E_DEPARTMENT");
        return employeeDao.selectList(wrapper);
    }

    @Override
    public List<Employee> getAllReport() {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.select("E_ID","E_NAME","E_JOB", "E_DEPARTMENT","E_ABSHOUR");
        return employeeDao.selectList(wrapper);
    }

    public Employee getInfoById(Integer EmployeeId) {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.select("E_ID", "E_NAME", "E_GENDER", "E_BIRTH", "E_START", "E_JOB", "E_DEPARTMENT");
        return employeeDao.selectById(EmployeeId);
    }

    public boolean deleteById(Integer EmployeeId) {
        return employeeDao.deleteById(EmployeeId) > 0;
    }

    public boolean modifyById(Employee employee) {
        return employeeDao.updateById(employee) > 0;
    }

    public boolean add(Employee employee) {
        // 添加员工把应有的假期算好
        // 传进来的参数有：、姓名、性别、出生日期、入职日期、职位、密码、部门
        // 自动算的参数有6种假期的剩余天数、扣除工资小时数为0、工号
        // 暂时为空的参数有：出勤状态、签到签退时间
        EmployeeFactory employeeFactory = new EmployeeFactory();
        Employee NewEmployee = employeeFactory.createrEmployee(employee.getE_JOB(), employee);
        return employeeDao.insert(NewEmployee) > 0;
    }

    @Override
    public boolean solveLeave(Integer isAgree, Absent absenter) {
        LambdaQueryWrapper<Absent> Awrapper = new LambdaQueryWrapper<>();
        Awrapper.eq(Absent::getA_ID, absenter.getA_ID());
        Absent absence = absentDao.selectOne(Awrapper);

        if (isAgree == 0) {
            absence.setA_RESPON(absenter.getA_REASON());
            absence.setA_STATE(4);
        }else{
            absence.setA_RESPON("Personnel audit approved");
            absence .setA_STATE(3);
            int days = absence.getA_ENDTIME().until(absence.getA_STTIME()).getDays();
            // 如果是因公请假，则不扣薪，只记录
            Employee nowperson = employeeDao.selectById(absence.getE_ID());
            if (absence.getA_TYPE() > 0) {// 如果是带薪假期，则只修改请的假的剩余天数
                int vocationType = absence.getA_TYPE();
                AbsentStrategySet absentStrategySet = new AbsentStrategySet();
                absentStrategySet.apply(vocationType, nowperson, days, absence);
                employeeDao.updateById(nowperson);
            }
        }
        absentDao.updateById(absence);
        return true;
    }

}
